﻿using System.Collections.Generic;
using umbraco.BusinessLogic;
using umbraco.cms.businesslogic.web;
using umbraco;
using umbraco.cms.businesslogic.template;
using umbraco.cms.businesslogic;
using umbraco.BasePages;

namespace ContentMaintenanceDashboard.Helpers
{
    /// <summary>
    /// Class responsible for all Umbraco actions.
    /// </summary>
    public class UmbracoHelper
    {
        /// <summary>
        /// Gets All Document Types from the system
        /// </summary>
        internal static List<DocumentType> GetAllContentTypes()
        {
            return DocumentType.GetAllAsList();
        }

        /// <summary>
        /// Gets All Templates from the system
        /// </summary>
        internal static List<Template> GetAllTemplates()
        {
            return Template.GetAllAsList();
        }

        /// <summary>
        /// Publish all Items in the SelectedIds list
        /// </summary>
        /// <param name="selectedIds">List of selected Id's</param>
        /// <param name="userId">The userId of the current logged on user</param>
        internal static void PublishAll(List<int> selectedIds)
        {
            foreach (var item in selectedIds)
            {
                PublishItem(item);
            }
        }

        /// <summary>
        /// UnPublish all Items in the SelectedIds list
        /// </summary>
        /// <param name="selectedIds">List of selected Id's</param>
        internal static void UnPublishAll(List<int> selectedIds)
        {
            foreach (var item in selectedIds)
            {
                UnPublishItem(item);
            }
        }

        /// <summary>
        /// Deletes all Items in the SelectedIds list
        /// </summary>
        /// <param name="selectedIds">List of selected Id's</param>
        internal static void DeleteAll(List<int> selectedIds)
        {
            foreach (var item in selectedIds)
            {
                DeleteItem(item);
            }
        }

        /// <summary>
        /// Deletes all Items in the SelectedIds list
        /// </summary>
        /// <param name="selectedIds">List of selected Id's</param>
        internal static bool MoveAll(List<int> selectedIds, int parentId)
        {
            bool allSucceeded = true;

            string parentPath = new CMSNode(parentId).Path;

            foreach (var item in selectedIds)
            {
                Document doc = new Document(item);
                if (! parentPath.StartsWith( doc.Path))
                {
                    //Only move when it's not already in the same parentfolder or it's own folder
                    doc.Move(parentId);
                }
                else
                {
                    //Not all items could be moved because it would result in a parent conflict
                    //you can't move a parent into it's child node
                    allSucceeded = false;
                }
            }

            return allSucceeded;
        }

        /// <summary>
        /// Publish a single Item
        /// </summary>
        /// <param name="id">The Id of the item that needs to be published</param>
        internal static void PublishItem(int id)
        {
            Document document = new Document(id);
            document.Publish(User.GetCurrent());
            library.UpdateDocumentCache(document.Id);
        }

        /// <summary>
        /// UnPublish a single Item
        /// </summary>
        /// <param name="id">The Id of the item that needs to be published</param>
        internal static void UnPublishItem(int id)
        {
            Document document = new Document(id);
            if (document.Published)
            {
                document.UnPublish();
                library.UnPublishSingleNode(document.Id);
            }
        }

        /// <summary>
        /// Delete a single Item
        /// </summary>
        /// <param name="id">The Id of the item that needs to be published</param>
        /// <remarks>If the Item is published it will be unpublished first</remarks>
        internal static void DeleteItem(int id)
        {
            Document document = new Document(id);
            if (document.Published)
            {
                //First UnPublish
                library.UnPublishSingleNode(document.Id);
                document.UnPublish();

            }
            document.delete();
        }
    }
}
